package org.jamin.lending.service.impl;

import javax.annotation.PostConstruct;

import org.apache.commons.codec.digest.DigestUtils;
import org.jamin.lending.domain.entity.Auser;
import org.jamin.lending.domain.form.LoginForm;
import org.jamin.lending.repository.AuserRepository;
import org.jamin.lending.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ManagerServiceImpl implements ManagerService {
	
	@Autowired
	private AuserRepository auserRepository;

	@Override
	public Auser adminLogin(LoginForm auser) {
		String password = org.apache.commons.codec.digest.DigestUtils.md5Hex(auser.getPassword());
		return this.auserRepository.findByUsernameAndPassword(auser.getUsername(), password);
	}

	@PostConstruct
	@Override
	public void initAdmin() {
		long count = this.auserRepository.count();
		if(count == 0){
			String password = DigestUtils.md5Hex("huang1100");
			Auser auser = new Auser();
			auser.setUsername("admin");
			auser.setPassword(password);
			this.auserRepository.save(auser);
		}
		
		
	}

}
